﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.Entity.CleanChina;
using System.Data.SqlClient;
using System.Data;

namespace Torch.WL.DataAccess.CleanChina
{
    public class TbNewsAccess
    {
        public TbNewsAccess()
        {

        }

        public List<TbNewsEntity> GetNewsList(int lessthan_newsid, int pagesize)
        {
            

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top "+ pagesize +" * from Tb_News ");
            strSql.Append(" where newsId<@lessthan_newsid order by newsId desc");
            SqlParameter[] parameters = {
					new SqlParameter("@lessthan_newsid", SqlDbType.Int,4)
			};
            parameters[0].Value = lessthan_newsid;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);
        }

        private List<TbNewsEntity> DataTableToList(DataTable dt)
        {
            List<TbNewsEntity> modelList = new List<TbNewsEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TbNewsEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }



        public TbNewsEntity GetModel(int newsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Tb_News ");
            strSql.Append(" where newsId=@newsId");
            SqlParameter[] parameters = {
					new SqlParameter("@newsId", SqlDbType.Int,4)
			};
            parameters[0].Value = newsId;

            TbNewsEntity model = new TbNewsEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        private TbNewsEntity DataRowToModel(DataRow row)
        {
            TbNewsEntity model = new TbNewsEntity();
            if (row != null)
            {
                if (row["newsId"] != null && row["newsId"].ToString() != "")
                {
                    model.newsId = int.Parse(row["newsId"].ToString());
                }
                if (row["classId"] != null && row["classId"].ToString() != "")
                {
                    model.classId = int.Parse(row["classId"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["newsDate"] != null && row["newsDate"].ToString() != "")
                {
                    model.newsDate = DateTime.Parse(row["newsDate"].ToString());
                }
                if (row["content"] != null)
                {
                    model.content = row["content"].ToString();
                }
                if (row["contentImg"] != null)
                {
                    model.contentImg = row["contentImg"].ToString();
                }
                if (row["hitTimes"] != null && row["hitTimes"].ToString() != "")
                {
                    model.hitTimes = int.Parse(row["hitTimes"].ToString());
                }
                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
            }
            return model;
        }



    }
}
